namespace two {
    export function addTap(ds: egret.DisplayObject, call: (e: egret.TouchEvent) => void, thisObj?: any) {
        ds.addEventListener(egret.TouchEvent.TOUCH_TAP, call, thisObj);
    }

    export function getStarName(starId: number) {
        return starId + "";
    }

    export function getCarema(call: (data) => void) {
        one.setTimeout(function () {
            call({});
        }, 100);
    }

    export function scan(): void {
        // checkCode("eJfJ5ZIdIfjVjJyJ");
        if (!isInWx()) {
            checkCode("eJfJ5ZIdIfjVjJyJ");
            return;
        }
        wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                var code = getOptionByUrl(getOpenKey(), result);
                // alert(code);
                if (code) {
                    checkCode(code);
                }
                else {
                    two.showTip("扫描二维码非对应首饰码，请重新扫描！")
                    // checkCode("eJfJ5ZIdIfjVjJyJ");
                }
            }
        });
    }

    export function checkCode(code: string): void {
        User_bindingJewelryProxy.getInstance().send({
            code: code
        }, (data) => {
            if (data.code == 0) {
                two.openStar(data.data.id);
            }
            else {
                // two.showTip("扫描二维码非对应首饰码，请重新扫描！")

                let box = two.showConfirm(data['msg']);//未成功
                box.once(egret.Event.REMOVED_FROM_STAGE, () => {
                    two.changePage("map");
                }, null);
            }
        })
    }

    export function openStar(jewId: string): void {
        let page = two.changePage("loading_jewelry") as JewelryLoadingPage;
        page.startLoad(jewId);
    }

    export function showJewPage(starId: string): void {

        User_getJewelryProxy.getInstance().send({
            id: starId
        }, (data) => {
            if (data.code == 0) {
                let page = two.changePage("jewelry") as two.JewelryPage;
                page.refreshData("onInitData", { id: starId });
            }
        })
    }

    export function getOpenKey(): string {
        return RES.getRes("gameCfg_json")['open']['name'];
    }

    export function getOpenCode(): string {
        let codeKey = getOpenKey();

        return one.getOption(codeKey);
    }

    export function getShareStarKey(): string {
        return RES.getRes("gameCfg_json")['share']['name'];;
    }

    export function getShareStarId(): string {
        let codeKey = two.getShareStarKey();

        return one.getOption(codeKey);
    }

    export function getPlatKey(): string {
        let codeKey = RES.getRes("gameCfg_json")['plat']['name'];

        return codeKey;
    }

    export function openPlatUrl(): void {
        let codeKey = RES.getRes("gameCfg_json")['plat']['name'];
        let plat = one.getOption(codeKey);

        let url = RES.getRes("gameCfg_json")['plat']['urls'][plat];
        if (!url) {
            url = RES.getRes("gameCfg_json")['plat']['urls'][RES.getRes("gameCfg_json")['plat']['default']];
        }

        window.location.href = url;
    }

    export function getOptionByUrl(key: string, url: string): string {
        if (url) {
            var search = url.match(/\?.*/);
            if (!search) {
                return "";
            }
            var searchArr = search[0].substr(1).split("&");
            var length_1 = searchArr.length;
            for (var i = 0; i < length_1; i++) {
                var str = searchArr[i];
                var arr = str.split("=");
                if (arr[0] == key) {
                    return arr[1];
                }
            }
        }
        return "";
    }

    export function openSound(isOpen: boolean, call: () => void) {
        let isWx = true;
        var music_mp3 = document.getElementById('music_mp3') as HTMLAudioElement;
        if (!isWx) {
            music_mp3.pause();
        }
        if (isOpen) {
            if (isWx) {
                var obj = { volume: music_mp3.volume };
                music_mp3.play();
            }
            else {
                var obj = { volume: 0 };
                one.soundManager.playMusic("bg");
                one.soundManager.setVolume(egret.Sound.MUSIC, obj.volume);
            }

            egret.Tween.get(obj, {
                onChange: (data) => {
                    if (isWx) {
                        music_mp3.volume = obj.volume;
                    }
                    else {
                        one.soundManager.setVolume(egret.Sound.MUSIC, obj.volume);
                    }
                }
            }).to({ volume: 1 }, 300).call(() => {
                call();
            });
        }
        else {
            var obj = { volume: 1 };
            if (isWx) {

            }
            else {
                one.soundManager.setVolume(egret.Sound.MUSIC, obj.volume);
            }

            egret.Tween.get(obj, {
                onChange: (data) => {
                    if (isWx) {
                        music_mp3.volume = obj.volume;
                    }
                    else {
                        one.soundManager.setVolume(egret.Sound.MUSIC, obj.volume);
                    }
                }
            }).to({ volume: 0 }, 300).call(() => {
                if (isWx) {
                    music_mp3.pause();
                }
                else {
                }
                call();
            });
        }
    }

    export function wxConfig(call: () => void): void {
        if (!isInWx()) {
            call();
            return;
        }
        wx.ready((e) => {
            call();
        })

        User_getSignatureProxy.getInstance().send({
            url: encodeURIComponent(window.location.href)
        }, (data) => {
            wx.config({
                debug: false,
                appId: data.data.appId,
                timestamp: data.data.timestamp,
                nonceStr: data.data.nonceStr,
                signature: data.data.signature,
                jsApiList: [
                    "updateAppMessageShareData",
                    "updateTimelineShareData",
                    "onMenuShareTimeline",
                    "onMenuShareAppMessage",
                    "onMenuShareQQ",
                    "onMenuShareQQ",
                    "onMenuShareWeibo",
                    "onMenuShareQZone",
                    "scanQRCode",
                ]
            })
        })
    }

    export function shareJepage(isStar: boolean, key: string): void {
        if (!isInWx()) {
            return;
        }
        let url = "http://pk23.g-demos.com/xingkong/User.login";

        let platKey = "";
        let shareInfo;
        if (RES.getRes("share_json") && RES.getRes("share_json")[key]) {
            shareInfo = RES.getRes("share_json")[key];
            platKey = two.getPlatKey();
        }
        else {
            shareInfo = {
                "title": "来点亮属于你的星空吧",
                "desc": "Helio赫粒联名AR天文科技品牌，AstroReality基于真实星图创造了，StarCollection系列首饰",
                "image": "https://res.mr4iot.com/1901/24061219045.png"
            }
            platKey = "plat";
        }

        let title: string = shareInfo.title;
        let desc: string = shareInfo.desc;
        let imgUrl: string = shareInfo.image;

        let link: string = "";
        if (isStar) {
            url = url + "?" + two.getShareStarKey() + "=" + key;
            link = url + "&" + platKey + "=" + one.getOption(platKey);
        }
        else {
            link = url + "?" + platKey + "=" + one.getOption(platKey);
        }

        wx.updateAppMessageShareData({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.updateTimelineShareData({
            title: title, // 分享标题
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.onMenuShareTimeline({
            title: title, // 分享标题
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.onMenuShareAppMessage({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.onMenuShareQQ({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.onMenuShareWeibo({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
        wx.onMenuShareQZone({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 设置成功
            }
        });
    }

    function isInWx(): boolean {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i)) {    //判断是否是微信环境
            return true;
        } else {
            return false;
            //非微信环境逻辑
        }
    }
}